package com.gfv.facturas.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.gfv.facturas.comandos.FacturasListadoCommand;
import com.gfv.facturas.negocio.Factura;

public class FacturasDAO implements RowMapper {
	protected final Log logger = LogFactory.getLog(getClass());

	private JdbcTemplate jdbcPlant;

	PersonasDAO persdao;

	ConceptosDAO concdao;

	public List consultar(FacturasListadoCommand patron, int uid) {
		StringBuffer sb = new StringBuffer();
		//StringBuffer where = new StringBuffer();
		ArrayList<Object> valores = new ArrayList<Object>();
		List facturas=null;
		sb.append("SELECT * FROM Facturas WHERE ");
		sb.append("recibida=? AND uid=?");
		valores.add(patron.getRecibida());
		valores.add(uid);
		if ("".equals(patron.getAnio()) == false) {
			sb.append(" AND anio=?");
			valores.add(Integer.parseInt(patron.getAnio()));
		}
		if ("".equals(patron.getTrimestre()) == false) {
			sb.append(" AND trimestre=?");
			valores.add(Integer.parseInt(patron.getTrimestre()));
		}
		if ("".equals(patron.getPersona()) == false) {
			sb.append(" AND terceroid=?");
			valores.add(Integer.parseInt(patron.getPersona()));
		}
		if ("".equals(patron.getNum_anot()) == false) {
			sb.append(" AND num_anot=?");
			valores.add(patron.getNum_anot());
		}
		try{
			facturas=jdbcPlant.query(sb.toString(),valores.toArray(), this);	
		}catch (Exception ex)
		{
			facturas = new ArrayList<Factura>();
		}
		return facturas;
	}
	private int IdNuevo()
	{
		int id=jdbcPlant.queryForInt("SELECT max(id) FROM Facturas");
		id++;
		return id;
	}
	public Factura consultar(int id, int uid) {
		StringBuffer sb = new StringBuffer();
		//StringBuffer where = new StringBuffer();
		ArrayList<Object> valores = new ArrayList<Object>();
		Factura fact=null;
		sb.append("SELECT * FROM Facturas WHERE ");
		sb.append("id=? AND uid=?");
		valores.add(id);
		valores.add(uid);
		try{
			fact=(Factura) jdbcPlant.queryForObject(sb.toString(),valores.toArray(), this);	
		}catch (Exception ex)
		{
			fact=null;
		}		
		return fact;

	}

	public Factura nuevo(Factura fact) {
		StringBuffer sb=new StringBuffer();
		sb.append("INSERT INTO facturas(");
		sb.append(" id, uid, num_anot, fecha,");
		sb.append(" total_bi, total_iva, total_irpf");
		sb.append("tercero_id, desc, recibida");
		sb.append("estado, total_liq, total_pdte, ");
		sb.append("anio, trimestre)");
		sb.append(" VALUES (");
		sb.append(" ?, ?, ?, ?,");
		sb.append(" ?, ?, ?");
		sb.append("?, ?, ?");
		sb.append("?, ?, ?, ");
		sb.append("?, ?)");
		return fact;

	}

	public void actualizar(Factura fact) {

	}

	public Object mapRow(ResultSet rs, int arg1) throws SQLException {
		int pers;
		Factura fact=new Factura();
		fact.setId(rs.getInt("id"));
		fact.setUid(rs.getInt("uid"));
		fact.setNum_anot(rs.getString("num_anot"));
		fact.setFecha(rs.getDate("fecha"));
		fact.setTotal_bi(rs.getBigDecimal("total_bi"));
		fact.setTotal_iva(rs.getBigDecimal("total_iva"));
		fact.setTotal_irpf(rs.getBigDecimal("total_irpf"));
		pers=rs.getInt("tercero_id");
		fact.setTercero_id(pers);
		fact.setPers(persdao.getDatUsuario(pers));
		fact.setDesc(rs.getString("desc"));
		fact.setRecibida(rs.getBoolean("recibida"));
		fact.setEstado(rs.getByte("estado"));
		fact.setTotal_liq(rs.getBigDecimal("total_liq"));
		fact.setTotal_pdte(rs.getBigDecimal("total_pdte"));
		fact.setAnio(rs.getInt("anio"));
		fact.setTrimestre(rs.getByte("trimestre"));
		fact.setConceptos(concdao.getLista(fact.getId()));
		return fact;
	}

	public void setDataSource(DataSource ds) {
		jdbcPlant = new JdbcTemplate(ds);
	}

	public PersonasDAO getPersdao() {
		return persdao;
	}

	public void setPersdao(PersonasDAO pers) {
		persdao = pers;
	}

	public ConceptosDAO getConcdao() {
		return concdao;
	}

	public void setConcdao(ConceptosDAO concdao) {
		this.concdao = concdao;
	}
}
